<template>
<el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'"
           :close-on-click-modal="false"
           :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll
           width="600px">
<el-row :gutter="15" class="">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="medium" label-width="150px" label-position="top" >
            <el-col :span="12" >
                <el-form-item  label="上传时间"   prop="todate" >
                    <el-input    v-model="dataForm.todate" placeholder="系统自动生成"  readonly >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12" >
                <el-form-item  label="上传者"   prop="userid" >
                    <el-input    v-model="dataForm.userid" placeholder="系统自动生成"  readonly >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="问题"   prop="question" >
                    <el-input    v-model="dataForm.question" placeholder="请输入"  :style='{"width":"100%"}' true  type="textarea"  :autosize='{"minRows":4,"maxRows":4}' >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="回答"   prop="answer" >
                    <JNPF-Quill    v-model="dataForm.answer" placeholder="请输入内容..." >

                </JNPF-Quill>
                </el-form-item>
            </el-col>
            <el-col :span="12" >
                <el-form-item  label="所属公司"   prop="company" >
                    <el-select    v-model="dataForm.company" placeholder="请选择"  clearable  :style='{"width":"100%"}' :multiple="false" >
                            <el-option v-for="(item, index) in companyOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12" >
                <el-form-item  label="所属岗位领域"   prop="field" >
                    <JNPFTreeSelect  @change="getdatabyname"  v-model="dataForm.field" placeholder="请选择"  clearable  :multiple="false"  :options="fieldOptions" :props="fieldProps" >

                </JNPFTreeSelect>
                </el-form-item>
            </el-col>
            <el-col :span="12" >
                <el-form-item  label="所属岗位类别"   prop="type" >
                    <JNPFTreeSelect    v-model="dataForm.type" placeholder="请选择"  clearable  :multiple="false"  :options="typeOptions" :props="typeProps" >

                </JNPFTreeSelect>
                </el-form-item>
            </el-col>
</el-form>
</el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false"> 取 消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail"> 确 定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    import request from '@/utils/request'
    import { previewDataInterface } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                loading: false,
                isDetail: false,
            dataForm: {
                            todate : "",
                            userid : "",
                            question : '',
                            answer : '',
                            company : "",
                            field : "",
                            type : "",
                            tonei : 1,
            },
            rules:
            {
                            question: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            answer: [
                                {
                                    required: true,
                                    message: '请输入内容...',
                                    trigger: 'blur'
                                },
                        ],
            },
                        companyOptions:[],
                        fieldOptions:[],
                        fieldProps:{"children":"children","label":"fullName","value":"id"},
                        typeOptions:[],
                        typeProps:{"children":"children","label":"fullName","value":"id"},

        }
        },
        computed: {},
        watch: {},
        created() {
                    this.getcompanyOptions();
                    this.getfieldOptions();
                    this.gettypeOptions();
        },
        mounted() {},
        methods: {
              getdatabyname(value)
                    {
                        this.selectdata = this.fieldOptions.find(option => option.id === value);
                        console.log('领域',this.selectdata.enCode);
                        getDictionaryDataSelector(this.selectdata.enCode).then(res => {
                            this.typeOptions = res.data.list
                        })
                    },
                    getcompanyOptions() {
                        getDictionaryDataSelector('b0441013ce1241c7bd81812199306f2a').then(res => {
                            this.companyOptions = res.data.list
                        })
                    },
                    getfieldOptions() {
                        getDictionaryDataSelector('90076a6023284cb1adc217fff660e6b5').then(res => {
                            this.fieldOptions = res.data.list
                        })
                    },
            init(id, isDetail) {
                this.dataForm.id = id || 0;
                this.visible = true;
                this.isDetail = isDetail || false;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/Whu_knowledgebase/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        })
                        this.getdatabyname(this.dataForm.field);
                    }

                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            this.request()
                    }
                })
            },
            request() {
                var _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/Whu_knowledgebase',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refresh', true)
                            }
                        })
                    })
                }else{
                    request({
                        url: '/api/example/Whu_knowledgebase/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refresh', true)
                            }
                        })
                    })
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
                this.dataForm = _dataAll
            },
        },
    }

</script>
